package com.hn.zhijian.utils;

import java.net.InetAddress;

/**
 * 生成类似hibernate中uuid 32位主键序列
 *
 * @author carl chen
 * @version: V1.0
 */
public class UUID {

	private static final int IP;

	public static int IptoInt(byte[] bytes) {
		int result = 0;
		for (int i = 0; i < 4; i++) {
			result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
		}
		return result;
	}

	static {
		int ipadd;
		try {
			ipadd = IptoInt(InetAddress.getLocalHost().getAddress());
		} catch (Exception e) {
			ipadd = 0;
		}
		IP = ipadd;
	}
	private static short counter = (short) 0;
	private static final int JVM = (int) (System.currentTimeMillis() >>> 8);

	public UUID() {

	}
	public static int getJVM() {
		return JVM;
	}
	public static short getCount() {
		synchronized (UUID.class) {
			if (counter < 0){
				counter = 0;
			}
			return counter++;
		}
	}
	public static int getIP() {
		return IP;
	}
	public static short getHiTime() {
		return (short) (System.currentTimeMillis() >>> 32);
	}

	public static int getLoTime() {
		return (int) System.currentTimeMillis();
	}

	private final static String sep = "";

	public static String format(int intval) {
		String formatted = Integer.toHexString(intval);
		StringBuffer buf = new StringBuffer("00000000");
		buf.replace(8 - formatted.length(), 8, formatted);
		return buf.toString();
	}

	public  static String format(short shortval) {
		String formatted = Integer.toHexString(shortval);
		StringBuffer buf = new StringBuffer("0000");
		buf.replace(4 - formatted.length(), 4, formatted);
		return buf.toString();
	}

	public static String generate() {
		return String.valueOf(new StringBuffer(36).append(format(getIP())).append(sep)
				.append(format(getJVM())).append(sep)
				.append(format(getHiTime())).append(sep)
				.append(format(getLoTime())).append(sep)
				.append(format(getCount())).toString());
	}
	public static void main(String args[]){
//		System.out.println(Dome.generate());
//		Calendar calendar = Calendar.getInstance();
//		System.out.println("现在："+calendar.getTime().getTime());
//		calendar.add(Calendar.YEAR, 10);
//		System.out.println("10年后："+calendar.getTime().getTime());
//
//		System.out.println(System.currentTimeMillis());
//		System.out.println(System.currentTimeMillis()+getShortUuid());
		System.out.println(getBatchCode());
	}

	/**
	 * 获取唯一订单ID
	 * @return
	 */
	public static String getUUIdNo(){
		return "TNBS"+DateUUIDUtil.getDateUUid()+getShortUuid();
	}

	/**
	 * 获取批次号
	 * @return
	 */
	public static String getBatchCode(){
		return "KY-HN-CS"+DateUUIDUtil.getDateUUid();
	}


	/**
	 * 获取唯一订单ID
	 * @return
	 */
	public static String getUUIdNo(String tge){
		return tge+DateUUIDUtil.getDateUUid()+getShortUuid();
	}

	/**
	 * 获取唯一ID
	 * @return
	 */
	public static String getUUId(){
		String s = java.util.UUID.randomUUID().toString();
		return s.substring(0,8)+s.substring(9,13)+s.substring(24);
	}

	public static String[] chars = new String[]
			{
			"1", "2", "3", "4", "5", "6",
			"7", "8", "9", "0", "1", "2", "3", "4", "5", "6", "7", "1", "9",
			"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2",
			"6", "7", "8", "9", "6", "7", "8", "9", "6", "7", "8", "9", "1",
			"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "1",
			"1", "2", "3", "4"
			};


	    public static String getShortUuid()
	    {
	        StringBuffer stringBuffer = new StringBuffer();
	        String uuid = java.util.UUID.randomUUID().toString().replace("-", "");
	        for (int i = 0; i < 8; i++)
	        {
	            String str = uuid.substring(i * 4, i * 4 + 4);
	            int strInteger  = Integer.parseInt(str, 16);
	            stringBuffer.append(chars[strInteger % 0x3E]);
	        }

	        //如果长度大于4就减掉最后一个
	        if(stringBuffer.length() > 4){
	        	stringBuffer.deleteCharAt(stringBuffer.length()-1);
	        }


	        return stringBuffer.toString();
	    }
}

